//
//  UTEPedMethodCallHandler.m
//  uteped_sdk
//
//  Created by mateng on 2024/1/13.
//

#import <UTEBluetoothRYApi/UTEBluetoothRYApi.h>
#import "UTEPedMethodCallHandler.h"
#import "UTEPedBleClientHelper.h"

#import "Const.h"
#import "CommonMessage.h"
#import "DeviceMessage.h"
#import "ContactMessage.h"
#import "DeviceOptionMessage.h"
#import "HomeMessage.h"
#import "MenstrualMessage.h"
#import "SedentaryMessage.h"
#import "SpaceMessage.h"
#import "SportsMessage.h"
#import "TimeMessage.h"
#import "UpgradeMessage.h"
#import "WatchFaceMessage.h"
#import "WeatherMessage.h"


@implementation UTEPedMethodCallHandler

static UTEPedMethodCallHandler* instance = nil;

+ (UTEPedMethodCallHandler*) shareInstance
{
    if ( instance == nil ) {
        instance = [[UTEPedMethodCallHandler alloc] init];
    }
    return instance;
}

- (void)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    eventHandler = [[UTEPedEventHandler alloc] initWithMessenger:registrar.messenger];
    [self initMessages];
}

- (void)initMessages {
    messageDict = [[NSMutableDictionary alloc] init];
    
    DeviceMessage *deviceMsg = [[DeviceMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:deviceMsg forKey:deviceMsg.getPath];
    [[UTEPedBleClientHelper shareInstance] addBluetoothDelegate: deviceMsg];
    
    CommonMessage *commonMsg = [[CommonMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:commonMsg forKey:commonMsg.getPath];
    
    ContactMessage *contactMsg = [[ContactMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:contactMsg forKey:contactMsg.getPath];
    
    DeviceOptionMessage *deviceOptionMsg = [[DeviceOptionMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:deviceOptionMsg forKey:deviceOptionMsg.getPath];
    
    HomeMessage *homeMsg = [[HomeMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:homeMsg forKey:homeMsg.getPath];
    
    MenstrualMessage *menstrualMsg = [[MenstrualMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:menstrualMsg forKey:menstrualMsg.getPath];
    
    SedentaryMessage *sedentaryMsg = [[SedentaryMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:sedentaryMsg forKey:sedentaryMsg.getPath];
    
    SpaceMessage *spaceMsg = [[SpaceMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:spaceMsg forKey:spaceMsg.getPath];
    
    SportsMessage *sportsMsg = [[SportsMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:sportsMsg forKey:sportsMsg.getPath];
    
    TimeMessage *timeMsg = [[TimeMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:timeMsg forKey:timeMsg.getPath];
    
    UpgradeMessage *upgradeMsg = [[UpgradeMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:upgradeMsg forKey:upgradeMsg.getPath];
    
    WatchFaceMessage *watchFaceMsg = [[WatchFaceMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:watchFaceMsg forKey:watchFaceMsg.getPath];
    
    WeatherMessage *weatherMsg = [[WeatherMessage alloc] initWithHandler:eventHandler];
    [messageDict setObject:weatherMsg forKey:weatherMsg.getPath];
    
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    BOOL consumed = NO;
    if ([@"getPlatformVersion" isEqualToString:call.method]) {
        result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
    } else {
        NSDictionary* dict = call.arguments;
        NSString* path = [dict objectForKey:KEY_PATH];
        if (path != nil) {
            BaseMessage *message = [messageDict objectForKey:path];
            if (message != nil) {
                NSString* method = [dict objectForKey:KEY_METHOD];
                consumed = [message methodCallConsumed:method params:dict result:result];
            }
        }
    }
    
    if (!consumed) {
        result(@YES);
    }
}

- (void)publishLocal:(NSString *)action value:(NSString *)value {
    [self->eventHandler publishEvent: [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"", KEY_PATH,
                                     @"localNotify", KEY_METHOD,
                                     action, KEY_ACTION,
                                     value, KEY_VALUE, nil] async:YES];
}

@end
